﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfMvvm
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
        }

        public string _name = "Jack";

        public string Name
        {
            get => _name;
            set
            {
                SetValue(ref _name, value);
                OnPropertyChanged("Description");
            }
        }

        public int _age = 30;

        public int Age
        {
            get => _age;
            set
            {
                SetValue(ref _age, value);
                OnPropertyChanged("Description");
            }
        }

        public string Description => $"{Name} is {Age} years old.";

        public void IncreasingAge(object sender, RoutedEventArgs e)
        {
            Age += 1;
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected bool SetValue<T>(ref T backingField, T value, [CallerMemberName] string propertyName = "")
        {
            if (EqualityComparer<T>.Default.Equals(backingField, value))
            {
                return false;
            }

            backingField = value;
            this.OnPropertyChanged(propertyName);
            return true;
        }

        #endregion
    }
}